<?php
/**
 * Created by PhpStorm.
 * User: yanggang
 * Date: 2018/5/23
 * Time: 下午1:25
 */

namespace app\admin\org;


use app\common\model\AdminAccount;
use app\common\model\AdminAppPower;

class Account
{

    public static function check_account($accout, $pwd, &$out)
    {
        $detail = AdminAccount::get(array("account" => $accout));
        if (empty($detail)) {
            $out = '账号或密码错误';
            return false;
        }
        $pwd = self::set_pwd($accout, $pwd);
        if ($pwd != $detail['password']) {
            $out = '账号或密码错误';
            return false;
        }
        if ($detail['status'] != 1) {
            $out = '账号被禁用';
            return false;
        }
        $out = array('account'=>$detail['account'],'real_name'=>$detail['real_name'],'power_id'=>$detail['power_id'],'power_bicycle_place_id'=>$detail['power_bicycle_place_id']);
        return true;
    }

    public static function set_pwd($account, $pwd)
    {
        return md5(md5($account) . $pwd . 'hahahahahahahahaha');
    }

    public static function edit_account($id, $account, $name, $pwd, $status, $level, &$out)
    {
        if (empty($account)) {
            $out = '账号不能为空';
            return false;
        }
        if ($id > 0) {
            $detail = AdminAccount::get($id);
            if (empty($detail)) {
                $out = 'ID ERR';
                return false;
            }
            $data = array();
            if ($detail['account'] != $account) {
                $info = AdminAccount::get(array('account' => $account));
                if (!empty($info)) {
                    $out = '与已有账号相同,请更换一个账号';
                    return false;
                }
                $data['account'] = $account;
            }
            if (!empty($pwd) && $detail['pwd'] != self::set_pwd($account, $pwd)) {
                if ($id == 1) {
                    $out = '系统管理员admin的密码不可以通过后台修改';
                    return false;
                }
                $data['pwd'] = self::set_pwd($account, $pwd);
            }
            if ($detail['name'] != $name) {
                $data['name'] = $name;
            }
            if ($detail['status'] != $status) {
                $data['status'] = $status;
            }
            if ($detail['level'] != $level) {
                $data['level'] = $level;
            }
            if (!empty($data)) {
                AdminAccount::update($data, array('id' => $id));
                $out = '更新成功';
                return true;
            }
            $out = '更新成功';
            return true;
        } else {
            if (empty($pwd)) {
                $out = '密码不能为空';
                return false;
            }
            $data = array();
            $data['account'] = $account;
            $data['pwd'] = self::set_pwd($account, $pwd);
            $data['name'] = $name;
            $data['status'] = $status;
            $data['level'] = $level;
            if (AdminAccount::create($data)) {
                $out = '新增成功';
                return true;
            }
            $out = '新增失败';
            return false;
        }
    }

    public static function get_menu()
    {
        $menu_list = array();
        $menu_list[] = array('id' => 1, 'cn' => 'index', 'name' => '首页', 'link' => url('admin/Index/index'), 'child' => array());
        $menu_list[] = array('id' => 2, 'cn' => 'noticeinfo', 'name' => '公告管理', 'link' => 'javascript:;', 'child' => array(
            array('id' => 201, 'name' => '攻略管理', 'link' => url('admin/NoticeInfo/strategy'), 'an' => 'strategy', 'cn' => 'noticeinfo'),
            array('id' => 202, 'name' => '公告管理', 'link' => url('admin/NoticeInfo/notice'), 'an' => 'notice', 'cn' => 'noticeinfo')
        ));
        $menu_list[] = array('id' => 3, 'cn' => 'user', 'name' => '用户', 'link' => 'javascript:;', 'child' => array(
            array('id' => 301, 'name' => '用户管理', 'link' => url('admin/User/index'), 'an' => 'index', 'cn' => 'user')
        ));
        $menu_list[] = array('id' => 4, 'cn' => 'shopinfo', 'name' => '商城管理', 'link' => 'javascript:;', 'child' => array(
            array('id' => 401, 'name' => '奇妙小集市管理', 'link' => url('admin/ShopInfo/shop'), 'an' => 'olist', 'cn' => 'shopinfo'),
        ));
        $menu_list[] = array('id' => 5, 'cn' => 'activity', 'name' => '活动管理', 'link' => 'javascript:;', 'child' => array(
            array('id' => 501, 'name' => '找朋友', 'link' => url('admin/activity/zpy'), 'an' => 'zpy', 'cn' => 'activity'),
            array('id' => 502, 'name' => '汉字华容道', 'link' => url('admin/activity/hzhrd'), 'an' => 'hzhrd', 'cn' => 'activity'),
            array('id' => 503, 'name' => '猜成语', 'link' => url('admin/activity/ccy'), 'an' => 'ccy', 'cn' => 'activity'),
        ));
        $menu_list[] = array('id' => 6, 'cn' => 'gift', 'name' => '礼品券管理', 'link' => 'javascript:;', 'child' => array(
            array('id' => 601, 'name' => '礼品券列表', 'link' => url('admin/gift/giftInfo'), 'an' => 'list', 'cn' => 'gift'),
            array('id' => 602, 'name' => '订单列表', 'link' => url('admin/gift/order'), 'an' => 'order', 'cn' => 'gift'),
        ));
        $menu_list[] = array('id' => 7, 'cn' => 'videoinfo', 'name' => '视频管理', 'link' => 'javascript:;', 'child' => array(
            array('id' => 701, 'name' => '视频列表', 'link' => url('admin/videoInfo/videolist'), 'an' => 'list', 'cn' => 'video'),
        ));

        $return = array();
        foreach ($menu_list as $k => $v) {
            $v1 = $v;
            unset($v1['child']);
            $return[$k] = $v1;
            foreach ($v['child'] as $_k => $_v) {
                $return[$k]['child'][] = $_v;
            }

        }
        $return = array_values($return);
        return $return;
    }
}